package com.yuhuihui.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Objects;
import java.util.Properties;

/**
 * 配置参数工具类
 *
 * @author yuhh
 * @date 2023-05-25 14:39:57
 */
public final class PropertiesUtil {

    private static final Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);

    /**
     * 默认配置文件名
     */
    private static final String PROPERTIES_DEFAULT = "application-other.yml";

    /**
     * 主配置文件名
     */
    public static final String PROPERTIES_MASTER = "config/application.yml";

    /**
     * 根据默认的配置文件名获取配置参数
     *
     * @return {@link Properties } 配置参数
     * @author yuhh
     * @date 2023-05-25 14:40:03
     */
    public static Properties getProperties(){
        return getProperties(PROPERTIES_DEFAULT);
    }

    /**
     * 根据配置文件名获取配置参数
     *
     * @param propertiesName 配置文件名
     * @return {@link Properties } 配置参数
     * @author yuhh
     * @date 2023-05-25 14:40:08
     */
    public static Properties getProperties(String propertiesName){
        Properties properties = new Properties();
        try {
            InputStream inputStream = InputStreamUtil.getInputStream(propertiesName);
            properties.load(inputStream);
            Objects.requireNonNull(inputStream).close();
        } catch (IOException e) {
            logger.error("获取配置参数出现异常", e);
        }
        return properties;
    }

    private PropertiesUtil(){

    }
}
